/* Copyright (C) 2011  Till Schoenbein mail: tschoenbein[at]gmail.com
	This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.*/
package de.shb.image;

import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.font.FontRenderContext;
import java.awt.font.GlyphVector;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.text.MessageFormat;
import java.text.StringCharacterIterator;

import javax.imageio.ImageIO;

import de.shb.dice.DiceResult;

public class ImageCreator {

	private static final String TEIL1 = "Es wurde {0} mal mit einem W{1} gewuerfelt!";
	private static final String TEIL2 = "Die Ergebnisse lauten: ";
	private static final String TEIL3 = "Gewuerfelt wurde im Topic: {2} und Post: {3} ";
	private static final String LINE4 = "am {4, date} um {4, time}.";

	private static final String ORIGFILENAME = "empty.jpg";

	private BufferedImage bi, biFiltered;
	int w, h;

	public File createImage(DiceResult diceResult, String filename,
			Object[] arguments) {
		File savedFile = new File("C:\\Users\\Till\\Documents\\" + filename+".jpg");
		File origFile = new File("C:\\Users\\Till\\Documents\\" + ORIGFILENAME);
		//TODO: Lokalen Pfad entfernen. Aktuell wegen falsch eingerichteten Tomcat
		System.out.println("INFO ImageCreator.createImage: origFile Path=" + origFile.getAbsolutePath());
		System.out.println("INFO ImageCreator.createImage: savedFile Name=" + savedFile.getName());
		
		try {
			bi = ImageIO.read(origFile);
			w = bi.getWidth(null);
			h = bi.getHeight(null);
			if (bi.getType() != BufferedImage.TYPE_INT_RGB) {
				BufferedImage bi2 = new BufferedImage(w, h,
						BufferedImage.TYPE_INT_RGB);
				Graphics big = bi2.getGraphics();
				big.drawImage(bi, 0, 0, null);
				biFiltered = bi = bi2;
			}
			Graphics2D graphics = biFiltered.createGraphics();
			// till.drawOval(0, 0, 50, 50);
			Font font = new Font("Arial", 2, 20);
			GlyphVector a = font.createGlyphVector(
					new FontRenderContext(null, false, false),
					new StringCharacterIterator(MessageFormat.format(TEIL1,
							(Object[]) arguments)));
			GlyphVector b = font.createGlyphVector(new FontRenderContext(null,
					false, false), new StringCharacterIterator(TEIL2
					+ diceResult.toString()));
			GlyphVector c = font.createGlyphVector(
					new FontRenderContext(null, false, false),
					new StringCharacterIterator(MessageFormat.format(TEIL3,
							(Object[]) arguments)));
			GlyphVector line4 = font.createGlyphVector(
					new FontRenderContext(null, false, false),
					new StringCharacterIterator(MessageFormat.format(LINE4,
							(Object[]) arguments)));
			graphics.drawGlyphVector(a, 10, 25);
			graphics.drawGlyphVector(b, 10, 46);
			graphics.drawGlyphVector(c, 10, 67);
			graphics.drawGlyphVector(line4, 10,88);
			
			ImageIO.write(biFiltered, "JPG", savedFile);
			System.out.println("INFO ImageCreator.createImage: savedFile Path=" + savedFile.getAbsolutePath());
			
			return savedFile;

		} catch (IOException e) {
			System.out.println("ERROR ImageCreator.createImage: Image could not be read");
			return null;
		}
	}

}
